﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Model;

namespace BehaviorModel
{
    /// <summary>
    /// Entity which accumulates free Requests and gives you access to them.
    /// </summary>
    public class RequestPool : IRequestPool
    {
        private static readonly ConcurrentQueue<Request> _freeRequests = new ConcurrentQueue<Request>();

        private static readonly IRequestPool _instance = new RequestPool();

        private RequestPool() { }

        public static IRequestPool GetInstance()
        {
            return _instance;
        }

        public Request GetFreeRequest()
        {
            return new Request();
            //lock (_freeRequests)
            //{
            //    return (0 == _freeRequests.Count) ? new Request() : PopFreeRequest();
            //}
        }

        private Request PopFreeRequest()
        {
            lock (_freeRequests)
            {
                Request request;
                _freeRequests.TryDequeue(out request);
                return request ?? new Request();
            }
        }

        public void ReturnRequest(Request request)
        {
            //lock (_freeRequests)
            //{
            //    request.Init();
            //    _freeRequests.Enqueue(request);
            //}
        }

    }
}
